package com.cyt.chattlibrary.message;

import android.net.Uri;
import android.os.Parcel;

import com.cyt.chattlibrary.message.model.MessageContent;
import com.cyt.chattlibrary.message.model.UserInfo;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author QiQi
 * @ClassName: VoiceMessage
 * @date 2018/10/22
 * @description
 */
public class VoiceMessage extends MessageContent {
    public static final Creator<VoiceMessage> CREATOR = new Creator<VoiceMessage>() {
        @Override
        public VoiceMessage createFromParcel(Parcel source) {
            return new VoiceMessage(source);
        }

        @Override
        public VoiceMessage[] newArray(int size) {
            return new VoiceMessage[size];
        }
    };
    private Uri mUri;
    private int mDuration;
    private String mBase64;

    public VoiceMessage(byte[] data) {
        try {
            String source = new String(data);
            JSONObject jsonObject = new JSONObject(source);
            if (jsonObject.has("duration")) {
                setDuration(jsonObject.optInt("duration"));
            }

            if (jsonObject.has("content")) {
                setBase64(jsonObject.optString("content"));
            }
            if (jsonObject.has("user")) {
                setUserInfo(parseJsonToUserInfo(jsonObject.getJSONObject("user")));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private VoiceMessage(Uri uri, int duration) {
        this.mUri = uri;
        this.mDuration = duration;
    }

    protected VoiceMessage(Parcel in) {
        this.mUri = in.readParcelable(Uri.class.getClassLoader());
        this.mDuration = in.readInt();
        this.mBase64 = in.readString();
        setUserInfo(in.readParcelable(UserInfo.class.getClassLoader()));
    }

    public static VoiceMessage obtain(Uri uri, int duration) {
        return new VoiceMessage(uri, duration);
    }

    public Uri getUri() {
        return mUri;
    }

    public void setUri(Uri mUri) {
        this.mUri = mUri;
    }

    public int getDuration() {
        return mDuration;
    }

    public void setDuration(int mDuration) {
        this.mDuration = mDuration;
    }

    public String getBase64() {
        return mBase64;
    }

    public void setBase64(String mBase64) {
        this.mBase64 = mBase64;
    }

    @Override
    public byte[] encode() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("content", mBase64);
            jsonObject.put("duration", this.mDuration);
            if (this.getJSONUserInfo() != null) {
                jsonObject.putOpt("user", getJSONUserInfo());
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        this.mBase64 = null;
        return jsonObject.toString().getBytes();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(this.mUri, flags);
        dest.writeInt(this.mDuration);
        dest.writeString(this.mBase64);
        dest.writeParcelable(getUserInfo(), flags);
    }
}
